package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.risk.common.utils.DateUtils;
import com.dz.risk.dao.ReportCalculatedVersionManagementDao;
import com.dz.risk.pojo.domain.ReportCalculatedVersionManagement;
import com.dz.risk.pojo.model.CommonPageResult;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.ReportCalculatedVersionManagementService;
import com.dz.risk.service.ReportFormsDataValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * @author Doke
 * @date 2021/9/9 13:42
 */
@Service
public class ReportCalculatedVersionManagementServiceImpl extends ServiceImpl<ReportCalculatedVersionManagementDao, ReportCalculatedVersionManagement> implements ReportCalculatedVersionManagementService {

    private final ReportCalculatedVersionManagementDao reportCalculatedVersionManagementDao;

    private final ReportFormsDataValueService reportFormsDataValueService;

    @Autowired
    public ReportCalculatedVersionManagementServiceImpl(ReportCalculatedVersionManagementDao reportCalculatedVersionManagementDao, ReportFormsDataValueService reportFormsDataValueService) {
        this.reportCalculatedVersionManagementDao = reportCalculatedVersionManagementDao;
        this.reportFormsDataValueService = reportFormsDataValueService;
    }

    /**
     * 根据报表ID跟计算版本删除相关报表计算信息
     *
     * @param reportFormsId   报表ID
     * @param calculatingTime 计算版本
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByReportFormsIdAndCalculatingTime(String reportFormsId, Date calculatingTime) {
        UpdateWrapper<ReportCalculatedVersionManagement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(ReportCalculatedVersionManagement::getReportFormsId, reportFormsId)
                .eq(ReportCalculatedVersionManagement::getCalculatedVersion, calculatingTime);
        //删除计算报表版本信息
        reportCalculatedVersionManagementDao.delete(updateWrapper);

        //删除计算报表数据值
        reportFormsDataValueService.deleteByReportFormsIdAndVersion(reportFormsId, calculatingTime);
    }

    /**
     * 根据参数查询报表的计算版本信息
     *
     * @param pageModel               分页模型
     * @param reportFormsSerialNumber 报表编码
     * @param reportFormsName         报表名称
     * @param calculatingTime         计算日期
     * @return 报表数据计算版本信息集合
     */
    @Override
    public CommonPageResult pageList(PageModel pageModel, String reportFormsSerialNumber, String reportFormsName, Date calculatingVersion) {
        String calculatingTime = null;
        if(Objects.nonNull(calculatingVersion)){
            calculatingTime = DateUtils.dateToStringYears(calculatingVersion);
        }

        //分页查询违约债卷信息
        QueryWrapper<ReportCalculatedVersionManagement> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(!StringUtils.isEmpty(reportFormsSerialNumber), ReportCalculatedVersionManagement::getReportFormsSerialNumber, reportFormsSerialNumber)
                .like(!StringUtils.isEmpty(reportFormsName), ReportCalculatedVersionManagement::getReportFormsName, reportFormsName)
                .like(!StringUtils.isEmpty(calculatingTime), ReportCalculatedVersionManagement::getCalculatedVersion, calculatingTime)
                .orderByDesc(ReportCalculatedVersionManagement::getCreatedTime);

        Page<ReportCalculatedVersionManagement> reportCalculatedVersionManagementPage = reportCalculatedVersionManagementDao.selectPage(new Page<>(pageModel.getPageNum(), pageModel.getPageSize()), wrapper);

        return CommonPageResult
                .builder()
                .data(reportCalculatedVersionManagementPage.getRecords())
                .pages(reportCalculatedVersionManagementPage.getPages())
                .rows(pageModel.getPageSize())
                .total(reportCalculatedVersionManagementPage.getTotal())
                .page(pageModel.getPageNum())
                .build();
    }

    /**
     * 根据报表编号删除报表计算版本信息
     *
     * @param reportFormsSerialNumber 报表编号
     * @return 删除的条数
     */
    @Override
    public int deleteByReportFormsSerialNumber(String reportFormsSerialNumber) {
        UpdateWrapper<ReportCalculatedVersionManagement> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(ReportCalculatedVersionManagement::getReportFormsSerialNumber, reportFormsSerialNumber);
        return reportCalculatedVersionManagementDao.delete(wrapper);
    }

    /**
     * 根据报表ID删除报表计算记录
     *
     * @param reportFormsId 报表ID
     * @return 删除得条数
     */
    @Override
    public int deleteByReportFormsId(String reportFormsId) {
        UpdateWrapper<ReportCalculatedVersionManagement> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(ReportCalculatedVersionManagement::getReportFormsId, reportFormsId);
        return reportCalculatedVersionManagementDao.delete(wrapper);
    }
}
